package com.fingard.dsp.basicbankdata.ebank;

import com.fingard.constant.Format;
import com.fingard.dsp.bank.directConfig.EBankSet;
import com.fingard.text.StringHelper;
import com.fingard.xml.XElement;

import java.math.BigDecimal;
import java.util.Date;

public class EBankUtil {

    public static void setEBankInfo(String p_commandCode, XElement p_srcXE, Object p_trgObj, EBankSet p_eBankSet, StringBuilder p_sb) {
        try {
            if (!("190805".equals(p_commandCode) || "198805".equals(p_commandCode))) {
                if (p_eBankSet == null || "1".equals(p_srcXE.getSingleTextNotNull("SameBnk")) || ((new BigDecimal(p_srcXE.getSingleTextNotNull("PayAmount")).compareTo(p_eBankSet.amtLimit) > 0) && !p_eBankSet.isForceEnable) || (!p_eBankSet.isEnable && !p_eBankSet.isForceEnable)) {
                    return;
                }
            }

            boolean hasMatch = false;
            String eBankCode = p_srcXE.getSingleTextNotNull("CNAPSCode");
            String eBankName = p_srcXE.getSingleTextNotNull("CNAPSName");
            String tmpMapKey = p_srcXE.getSingleTextNotNull("RecBankCode");
            if (StringHelper.isNullOrEmpty(tmpMapKey)) {
                tmpMapKey = p_srcXE.getSingleTextNotNull("RecBankName");
            }
            String tmpATSAreaName = p_srcXE.getSingleTextNotNull("RecStandardArea");

            String[] tmpRet = EBankData.mappingEBankCode(tmpMapKey, tmpATSAreaName);

            if (StringHelper.hasAnyChar(tmpRet[0])) {
                hasMatch = true;
                eBankCode = tmpRet[0];
            }
            if (StringHelper.hasAnyChar(tmpRet[1])) {
                eBankName = tmpRet[1];
            }


            if (!hasMatch) {
                p_sb.append(Format.DateTimeSSSFormat.format(new Date()) + " " + tmpMapKey + "未在EBankMapping.xml中找到，不采用网银跨行付" + "\r\n");
                setTrgObjValue(p_trgObj, p_eBankSet.cnapsCodeSetNodeName, eBankCode, p_sb);
                setTrgObjValue(p_trgObj, p_eBankSet.cnapsNameSetNodeName, eBankName, p_sb);
            } else {
                setTrgObjValue(p_trgObj, p_eBankSet.cnapsCodeSetNodeName, "", p_sb);
                setTrgObjValue(p_trgObj, p_eBankSet.cnapsNameSetNodeName, "", p_sb);
                setTrgObjValue(p_trgObj, p_eBankSet.payTypeSetNodeName, p_eBankSet.payTypeSetNodeValue, p_sb);
                setTrgObjValue(p_trgObj, p_eBankSet.eBankCodeSetNodeName, eBankCode, p_sb);
                setTrgObjValue(p_trgObj, p_eBankSet.eBankNameSetNodeName, eBankName, p_sb);
            }
        } catch (Exception ex) {
            ex.printStackTrace();
            p_sb.append(Format.DateTimeSSSFormat.format(new Date()) + " " + "超级网银匹配失败，失败原因：" + ex.getMessage() + "\r\n");
        }

    }

    private static void setTrgObjValue(Object p_trgObj, String p_fieldName, String p_setVale, StringBuilder p_sb) {
        if (p_trgObj == null || StringHelper.isNullOrEmpty(p_fieldName)) {
            return;
        }
        String[] eachName = p_fieldName.split(",");
        if (p_trgObj instanceof String[]) {
            String[] tmptrgObj = (String[]) p_trgObj;

            for (String each : eachName) {
                try {
                    int tmpTrgNodeIndex = Integer.parseInt(each);
                    if (tmptrgObj.length >= tmpTrgNodeIndex) {
                        tmptrgObj[tmpTrgNodeIndex] = p_setVale;
                    } else {
                        p_sb.append(Format.DateTimeSSSFormat.format(new Date()) + " " + "节点解析配置有误，不存在来源节点" + each + "\r\n");
                    }
                } catch (Exception ex) {
                    p_sb.append(Format.DateTimeSSSFormat.format(new Date()) + " " + "节点解析配置有误，失败原因：" + ex.getMessage() + "\r\n");
                }
            }
        } else if (p_trgObj instanceof XElement) {
            XElement tmpTrgXe = (XElement) p_trgObj;
            XElement tmpNodeSource = tmpTrgXe;
            for (String each : eachName) {
                String[] eachNode = each.split("/");
                for (String nodeName : eachNode) {
                    tmpNodeSource = tmpNodeSource.getSingleChild(nodeName);
                    if (tmpNodeSource == null) {
                        break;
                    }
                }
                if (tmpNodeSource != null) {
                    tmpNodeSource.innerText = p_setVale;
                } else {
                    p_sb.append(Format.DateTimeSSSFormat.format(new Date()) + " " + "节点解析配置有误，不存在来源节点" + each + "\r\n");
                }
            }
        }
    }


}
